// _Exercise4_BackgroundSubstraction2_Avg.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
	cvNamedWindow("Sample",CV_WINDOW_AUTOSIZE);
//	cvNamedWindow("GreyScale",CV_WINDOW_AUTOSIZE);
	cvNamedWindow("Subtraction",CV_WINDOW_AUTOSIZE);

	CvCapture* capture= cvCreateFileCapture("D:\\Leavy4a_dataset.avi");
	IplImage* frame = cvQueryFrame(capture);
	

	if(!frame)
	{
		printf("Error in open file");
		return 0;
	}
	int i = 0;
	AllocateImages(frame);

	int fps = cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
	printf("\nfps: %d\n",fps);

	while( i < 100)
	{
		accumulateBackground(frame);
		frame = cvQueryFrame(capture);
		i++;
	}

	createModelsfromStats(12.0,13.0);

	cvSetCaptureProperty(capture,CV_CAP_PROP_POS_FRAMES,0);


	frame = cvQueryFrame(capture);

	while(frame)
	{
		cvShowImage("Sample",frame);
		IplImage* img_out = cvCreateImage(cvSize(frame->width,frame->height),frame->depth,1);
		backgroundDiff(frame,img_out);
		cvShowImage("Subtraction",img_out);

		char c = cvWaitKey(fps);
		if(c==27)
			break;
		frame = cvQueryFrame(capture);

	}


//	cvReleaseImage(&frame);
/*	DeallocateImages();*/
	cvReleaseCapture(&capture);
	cvDestroyWindow("Sample");
	cvDestroyWindow("Subtraction");

	return 0;
}

